function validateRegistration(formName){
	var form = document.forms[formName];
	if (form.elements['regUserName'].value == "" || form.elements['regPassword'].value == "" || 
		form.elements['regName'].value == "" || form.elements['regFamily'].value == "" || 
		form.elements['regPhone'].value == ""){
		alert("Missing fields");
		return false;
	}
	if (!isValidPhone(form.elements['regPhone'].value, 'Please input a valid phone number')){
		return false;
	}
	return true;
}

function validateAddCourse(formName){
	var form = document.forms[formName];
	if (form.elements['courseName'].value == "" || form.elements['description'].value == "" || 
		form.elements['numOfpts'].value == "" || form.elements['capacity'].value == "" || 
		form.elements['groupNum'].value == ""){
		alert("Missing fields");
		return false;
	}
	if (!isValidNonZeroPositive(form.elements['numOfpts'].value, "Please input a valid number of points")){
		return false;
	}
	if (!isValidNonZeroPositive(form.elements['capacity'].value, "Please input a valid capacity")){
		return false;
	}
	if (!isValidNonZeroPositive(form.elements['groupNum'].value, "Please input a valid groupNum")){
		return false;
	}
	return true;
}

function isValidNonZeroPositive(str, error){
	var r = /([^0-9])/;
	if (r.test(str) || str == 0) {
		alert(error);
		return false;
	}
	return true;
}

function validateLogin(){
	var form = document.forms['Login'];
	if (form.elements['loginUserName'].value == "" || form.elements['loginPassword'].value == ""){
		alert("Missing fields");
		return false;
	}
	return true;
}

function isValidPhone(str, error) {
	var r = /([^0-9])/;
	if (r.test(str)) {
		alert(error);
		return false;
	}
	var r = /([0-9]{7})/;
	if (!r.test(str)) {
		alert(error);
		return false;
	}
	return true;
}

function resizeWin(){
	var winHeight = document.getElementById('bodyArea').offsetHeight;
	var winWidth = document.getElementById('bodyArea').offsetWidth;
	window.resizeTo(winWidth+8,winHeight+80);
}

function addRow(tableID) {
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	var colCount = table.rows[0].cells.length;

	for(var i=0; i < colCount; i++) {
		var newcell = row.insertCell(i);
		newcell.innerHTML = table.rows[1].cells[i].innerHTML;
		//alert(newcell.childNodes);
		switch(newcell.childNodes[0].type) {     
			case "checkbox":
				newcell.childNodes[0].checked = false;
				break;
			case "select-one":
				newcell.childNodes[0].selectedIndex = 0;
				break;
		}
	}
}

function deleteRow(tableID) {
	try {
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;

		for(var i=1; i<rowCount; i++){
			var row = table.rows[i];
			var chkbox = row.cells[0].childNodes[0];
			if (null != chkbox && true == chkbox.checked){
				if(rowCount <= 2){
					alert("Cannot delete all the rows.");
					break;
				}
				table.deleteRow(i);
				rowCount--;
				i--;
			}
    	}
	}catch(e){
		alert(e);
	}
}

function getRequestObject() {
	if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else
		return null;
}

function doSearch() {
	var request = new XMLHttpRequest();
	request.open("GET", encodeURI("/skiryanoandspuzir/Search?searchName="
			+ document.getElementById('courseName').value + "&searchDesc="
			+ document.getElementById('description').value), true);
	request.onreadystatechange = function() {
		if (request.readyState == 4 && request.status == 200) {
			document.getElementById("results").innerHTML = request.responseText;
		}
	};
	request.send(null);
}